<?php
namespace App\DBs\Stock;

use Illuminate\Support\Facades\DB;

class LayerDB extends BaseDB
{
    /**
     * 素材字段
     * vid 库视频ID
     * serial 编号顺序
     * type 1素材地址，2文字，3特效，
     * link 素材地址
     * style CSS样式
     */

    protected $table = 'stock_layer';

    public function __construct()
    {
    }

    public function getTypes()
    {
        return array(
            1 => '素材地址', '文字', '特效',
        );
    }

    /**
     * ADMIN列表
     */
    public function getAdminIndex($page, $size, $vid)
    {
        $query = DB::table($this->table)->where('del',0);
        if ($vid) { $query = $query->where('vid',$vid); }
        $queryTotal = $query->count();
        $models = $query->orderBy('vid','desc')
            ->orderBy('serial','asc')
            ->skip($page)->take($size)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有时间');
        }
        $models = $this->jsonToArr($models);
        $dataArr = array();
        foreach ($models as $model) {
            $dataArr[] = [
                'id' => $model['id'],
                'serial' => $model['serial'],
                'type' => $model['type'],
                'link' => $model['link'],
                'style' => $model['style'],
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr, 'total'=>$queryTotal);
    }

    /**
     * ADMIN一个
     */
    public function getAdminOne($vid)
    {
        $models = DB::table($this->table)->where('del',0)
            ->where('vid',$vid)
            ->orderBy('id','desc')
            ->skip(0)->take(1)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        $dataArr = [
            'id' => $model['id'],
            'serial' => $model['serial'],
            'type' => $model['type'],
            'link' => $model['link'],
            'style' => $model['style'],
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * ADMIN最大一个
     */
    public function getAdminMaxOne($vid)
    {
        $models = DB::table($this->table)->where('del',0)
            ->where('vid',$vid)
            ->orderBy('serial','desc')
            ->skip(0)->take(1)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        $dataArr = [
            'id' => $model['id'],
            'serial' => $model['serial'],
            'type' => $model['type'],
            'link' => $model['link'],
            'style' => $model['style'],
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * ADMIN新增
     * @param $itams 更新的数据
     */
    public function setAdminAdd($items)
    {
        if (!$items) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        $items['created_at'] = time();
        //开启事务
        DB::beginTransaction();
        try {
            //去除老数据
            $lastId = DB::table($this->table)->insertGetId($items);
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * ADMIN更新
     * @param $id 素材ID
     * @param $itams 更新的数据
     */
    public function setAdminUpdate($id, $items)
    {
        if (!$id || !$items) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        $items['updated_at'] = time();
        //开启事务
        DB::beginTransaction();
        try {
            //去除老数据
            DB::table($this->table)->where('id', $id)->update($items);
            $dataArr = array();
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * APP列表
     */
    public function getAppIndex($uid, $bid)
    {
        //库信息
        $resStocks = DB::table($this->tStock)->where('del',0)
            ->where('id',0)->get();
        //素材列表
        $query = DB::table($this->table)->where('del',0)
            ->where('bid',$bid)
            ->where('uid',$uid);
        $queryTotal = $query->count();
        $models = $query->orderBy('serial','asc')->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $models = $this->jsonToArr($models);
        $dataArr = array();
        foreach ($models as $model) {
            $link = $model['type']==1 ? env('DOMAIN').$model['link'] : $model['link'];
            $dataArr[] = [
                'serial' => $model['serial'],
                'type' => $model['type'],
                'link' => $link,
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr, 'total'=>$queryTotal);
    }
}